<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Admin_Paymentmethods extends Admin_Controller {
	public function __construct()
	{
		parent::__construct();
		$this->lang->load('paymentmethod');
	}
	public function ajax_paymentmethod_get(){
		$paymentmethodEntity =  new CI_Payment_method_entity();
		$paymentmethod = $paymentmethodEntity->getPaymentMethodByCode($_REQUEST['code']);
		echo json_encode($paymentmethod);
	}
	public function ajax_paymentmethod_list(){
		$paymentmethodEntity =  new CI_Payment_method_entity();
		$escortSettings = new CI_Escort_setting_entity();
		$defaultLanguageId = $escortSettings->getDefaultLanguageId();
		$paymentmethodList = $paymentmethodEntity->getPaymentMethodListByLanguage($defaultLanguageId);
		$data['paymentmethodList'] = $paymentmethodList;
		
		$languageEntity = new CI_Language_entity();
		$languageList = $languageEntity->getLanguageList();
		$data['languageList'] = $languageList;
		
		$this->load->view('admin/paymentmethods/index', $data);
	}
	public function ajax_paymentmethod_save(){
		$paymentmethodNameList = $_REQUEST['paymentmethod'];
		$paymentmethodNameDefault = '';
		foreach ($paymentmethodNameList as $key => $value) {
			if($value != null && $value != ""){
				$paymentmethodNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($paymentmethodNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_paymentmethod_require');
		}else{
			$code = 0;
			foreach ($paymentmethodNameList as $key => $value) {
				$paymentmethodEntity =  new CI_Payment_method_entity();
				if($value != null && $value != ''){
					$paymentmethodEntity->setMethod($value);
				}else{
					$paymentmethodEntity->setMethod($paymentmethodNameDefault);
				}
				$paymentmethodEntity->setLanguageId($key);
				$paymentmethodEntity->setCode($code);
				$id = $paymentmethodEntity->save();
				if($code == 0){
					$code = $id;
					$paymentmethodEntity->setId($id);
					$paymentmethodEntity->setCode($code);
					$paymentmethodEntity->update();
				}
			}
			$result['result'] = true;
			$result['message'] =lang('escort_paymentmethod_save_success');
			$result['id'] =$id;
		}
		echo json_encode($result);
	}
	public function ajax_paymentmethod_update(){
		$paymentmethodNameList = $_REQUEST['paymentmethod'];
		$paymentmethodIdList = $_REQUEST['paymentmethod_id'];
		$paymentmethodCode = $_REQUEST['paymentmethod_code'];
		$paymentmethodNameDefault = '';
		foreach ($paymentmethodNameList as $key => $value) {
			if($value != null && $value != ""){
				$paymentmethodNameDefault = $value;
				break;
			}
		}
		$result = array();
		if($paymentmethodNameDefault == ''){
			$result['result'] = false;
			$result['message'] = lang('escort_paymentmethod_require');
		}else{
			foreach ($paymentmethodNameList as $key => $value) {
				$paymentmethodEntity =  new CI_Payment_method_entity();
				if($value != null && $value != ''){
					$paymentmethodEntity->setMethod($value);
				}else{
					$paymentmethodEntity->setMethod($paymentmethodNameDefault);
				}
				$paymentmethodEntity->setLanguageId($key);
				$paymentmethodEntity->setCode($paymentmethodCode);
				$paymentmethodEntity->setId($paymentmethodIdList[$key]);
				$paymentmethodEntity->update();
			}
			$result['result'] = true;
			$result['message'] =lang('escort_paymentmethod_save_success');
		}
		
		echo json_encode($result);
	}
	public function ajax_paymentmethod_delete(){
		$result = array();
		$paymentmethodEntity =  new CI_Payment_method_entity();
		$result['result'] = $paymentmethodEntity->deleteByCode($_REQUEST['code']);
		if($result['result']){
			$result['message'] = lang('escort_paymentmethod_delete_success');
		}else{
			$result['message'] = lang('escort_paymentmethod_delete_error');
		}
		echo json_encode($result);
	}
}
?>